<?php
/**
 * <pre>
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT      : rubik-base
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 *
 * [GENERAL INFO OF THIS FILE]
 *
 * </pre>
 * 
 * @version 		1.0
 * @author			Dang Nhon Hoa <hoadn@rubik.com.vn>
 * @access			public
 * @package			[base.helper]
 *
 * Created on	: May 25, 2008
 */

RCoreImporter::cls("helper.CImage" );

class RImageHelper {
	
	/**
	 * Resize the image
	 *
	 * @static
	 * @param string $sImagePath file path of the image
	 * @param int $wScale percent of width scale
	 * @param int $hScale percent of width scale
	 * @return array new size {"h"=>height, "w"=>width}
	 */
	static function imageResize($sImagePath, $wScale = '100', $hScale = '') {
		if (empty ( $hScale ))
			$hScale = $wScale;
		$newSize = array ();
		$size = getimagesize ( $sImagePath );
		if ($size [0] > $size [1]) {
			if ($size [0] > $wScale)
				$newSize ['w'] = $wScale;
			else
				$newSize ['w'] = $size [0];
			$newSize ['h'] = round ( $newSize ['w'] * $size [1] / $size [0] );
			if ($newSize ['h'] > $hScale) {
				$olderSize = $newSize;
				$newSize ['h'] = $hScale;
				$newSize ['w'] = round ( $newSize ['h'] * $olderSize ['w'] / $olderSize ['h'] );
			}
			// Add New
		} else {
			if ($size [1] > $hScale)
				$newSize ['h'] = $hScale;
			else
				$newSize ['h'] = $size [1];
			$newSize ['w'] = round ( $newSize ['h'] * $size [0] / $size [1] );
			if ($newSize ['w'] > $wScale) {
				$olderSize = $newSize;
				$newSize ['w'] = $hScale;
				$newSize ['h'] = round ( $newSize ['w'] * $olderSize ['h'] / $olderSize ['w'] );
			}
		}
		return $newSize;
	}
	
	/**
	 * Create the thumbnail from a image
	 *
	 * @param string $sImagePath originally image path
	 * @param string $thumbPath expected thumbPath
	 * @param int $wScale width scale
	 * @param int $hScale height scale
	 */
	static function createThumbnail($sImagePath, $thumbPath, $wScale = '100', $hScale = '') {
		$typeFile = array ("jpg", "jpeg", "png", "gif", "bmp" );
		if (! in_array ( strtolower ( substr ( $sImagePath, - 3 ) ), $typeFile ) && ! in_array ( strtolower ( substr ( $sImagePath, - 4 ) ), $typeFile ))
			return 'none';
		$sOriginalFileName = $sImagePath;
		$sExtension = substr ( $sImagePath, (strrpos ( $sImagePath, '.' ) + 1) );
		$sExtension = strtolower ( $sExtension );
		switch ($sExtension) {
			case 'jpg' :
			case 'jpeg' :
				if (function_exists ( "imagejpeg" )) {
					$srcImg = imagecreatefromjpeg ( "$sImagePath" ); // or die( "File Not Found" );
					$sizeImage = CImage::imageResize ( $sImagePath, $wScale, $hScale );
					$thumbImg = imagecreatetruecolor ( $sizeImage ['w'], $sizeImage ['h'] );
					imagecopyresized ( $thumbImg, $srcImg, 0, 0, 0, 0, $sizeImage ['w'], $sizeImage ['h'], imagesx ( $srcImg ), imagesy ( $srcImg ) );
					imagejpeg ( $thumbImg, "$thumbPath" );
				}
				break;
			case 'gif' :
				if (function_exists ( "imagegif" )) {
					$srcImg = imagecreatefromgif ( "$sImagePath" ); // or die( "File Not Found" );
					$sizeImage = CImage::imageResize ( $sImagePath, $wScale, $hScale );
					$thumbImg = imagecreatetruecolor ( $sizeImage ['w'], $sizeImage ['h'] );
					imagecopyresized ( $thumbImg, $srcImg, 0, 0, 0, 0, $sizeImage ['w'], $sizeImage ['h'], imagesx ( $srcImg ), imagesy ( $srcImg ) );
					imagegif ( $thumbImg, "$thumbPath" );
				}
				break;
			case 'png' :
				if (function_exists ( "imagepng" )) {
					$srcImg = imagecreatefrompng ( "$sImagePath" ); // or die( "File Not Found" );
					$sizeImage = CImage::imageResize ( $sImagePath, $wScale, $hScale );
					$thumbImg = imagecreatetruecolor ( $sizeImage ['w'], $sizeImage ['h'] );
					imagecopyresized ( $thumbImg, $srcImg, 0, 0, 0, 0, $sizeImage ['w'], $sizeImage ['h'], imagesx ( $srcImg ), imagesy ( $srcImg ) );
					imagepng ( $thumbImg, "$thumbPath" );
				}
				break;
			case 'bmp' :
				if (function_exists ( "imagewbmp" )) {
					$srcImg = imagecreatefromwbmp ( "$sImagePath" ); // or die( "File Not Found" );
					$sizeImage = CImage::imageResize ( $sImagePath, $wScale, $hScale );
					$thumbImg = imagecreatetruecolor ( $sizeImage ['w'], $sizeImage ['h'] );
					imagecopyresized ( $thumbImg, $srcImg, 0, 0, 0, 0, $sizeImage ['w'], $sizeImage ['h'], imagesx ( $srcImg ), imagesy ( $srcImg ) );
					imagewbmp ( $thumbImg, "$thumbPath" );
				}
				break;
			default :
				return error_log ( "GD library does not support in this PHP server" );
				break;
		}
	
	}
	
	/**
	 * IF thumbnail isn't exists --> create a thumbnail
	 * ELSE return that thumbnail <br/>
	 * WARNING: becarefull when using this static function because when we change the originally picture 
	 * 		with the new one (same file name) ==> thumbnail still of the old one.
	 *
	 * @param string $sImagePath the file path that contain picture to create thumbnail
	 * @param string/int $wScale
	 * @param string/int $hScale
	 * @return string the thumbnail file path
	 */
	static function createCacheGetThumbnail($sImagePath, $wScale = '100', $hScale = '', $override = false) {
		RCoreImporter::cls("helper.RFileFolderHelper" );
		$thumbExt = RFileFolderHelper::getFileExtention ( $sImagePath );
		$thumbName = RFileFolderHelper::removeExtension ( $sImagePath );
		$thumbPath = $thumbName . "_thumb_$wScale" . "x$hScale" . ".$thumbExt";
		
		if (! $override && file_exists ( $thumbPath )) {
			return $thumbPath;
		}
		
		RImageHelper::createThumbnail ( $sImagePath, $thumbPath, $wScale, $hScale );
		return $thumbPath;
	}

}
?>